package com.guigu.xm.lq.controller;

import com.guigu.xm.pojo.SysUsers;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor implements HandlerInterceptor {
    //controller执行前调用此方法
    //返回true表示继续执行,返回false中止执行
    //可以经行登陆检验,权限拦截
    @Override
    public boolean preHandle(
            HttpServletRequest request,
            HttpServletResponse response,
            Object handler) throws Exception {
        SysUsers xx = (SysUsers) request.getSession().getAttribute("user");
        String method = request.getRequestURI();
        String servletPath = request.getServletPath();
        if(xx==null&&!"/suc/login".equals(method)&&!"/suc/login".equals(servletPath)){
            response.sendRedirect("/login.jsp");
            return false;
        }else{
            return true;
        }

    }
    //controller执行后 但未返回视图前 调用此方法
    //这里可返回用户前对模型数据进行加工处理
    //比如这里加入 公用信息 以便页面显示
    @Override
    public void postHandle(
            HttpServletRequest request,
            HttpServletResponse response,
            Object handler,
            ModelAndView modelAndView) throws Exception {
    }


    //controller执行后且视图返回后调用此方法
    //这里可以得到controller时的异常信息
    //这里可以操作日志文件
    @Override
    public void afterCompletion(
            HttpServletRequest request,
            HttpServletResponse response,
            Object handler,
            Exception ex) throws Exception {
    }
}
